<?php

class CategoryController extends \BaseController {

	/**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function index()
  {          
      
      $model = new Category; 
      $cate_type_id = Input::get('cate_type_id',1);
      $parent_id = Input::get('parent_id',null);
      $query = $model->where('cate_type_id','=', $cate_type_id);
      if ($cate_type_id) {
          $query->where('cate_type_id','=',$cate_type_id);
      }
      if ($parent_id) {
          $query->where('parent_id','=',$parent_id);
      }

      $dataArr= $query->orderBy('created_at','asc')->paginate(50);
   
      return View::make('backend.category.index')->with(array('dataArr'=>$dataArr));

  }

  /**
   * Show the form for creating a new resource.
   *
   * @return Response
   */
  public function create()
  {
      $cateTypeArr = CateType::all();
      $cateArr = Category::all();
      return View::make('backend.category.create')->with(
          array(
              'cateArr'=>$cateArr,
              'cateTypeArr'=>$cateTypeArr
          ));    
  }

  /**
   * Store a newly created resource in storage.
   *
   * @return Response
   */
  public function store()
  {
      $dataArr = Input::except('_token');

      $ruleArr = array(
        "cate_name" => "required"        
      );   

      $valid = Validator::make($dataArr,$ruleArr);

      if ($valid->passes()) {

          $model= new Category;
          $cate_name = Input::get('cate_name');
          $model->cate_name = $cate_name;
          $model->cate_alias = Input::get('cate_alias',null);
          $model->cate_type_id = Input::get('cate_type_id');
          $model->parent_id = Input::get('parent_id',0);
          $model->image_url = str_replace('../','',Input::get('image_url',null));  
          $model->content = Input::get('content',null);  
          $model->display_order = Input::get('display_order',1);  
          $model->is_hot = Input::get('is_hot',0);          
          $model->meta_title = Input::get('meta_title',$cate_name);
          $model->meta_description = Input::get('meta_description',$cate_name);
          $model->meta_keyword = Input::get('meta_keyword',$cate_name);
          $model->save();

          return Redirect::route('backend.category.index');

      } else {
         return Redirect::route('backend.category.create')->withErrors($valid);
      }     
  }

  /**
   * check code pattern
   *
   * @param  string  $code
   * @return boolean
   */
  private function regxCode($code) {

      $regx = '/^[A-Za-z0-9\-]+$/';
    
      if (preg_match($regx, $code)) {
          return true;
      } else {
          return false;
      }

  }
  

  /**
   * Show the form for editing the specified resource.
   *
   * @param  int  $id
   * @return Response
   */
  public function edit($id)
  {      
      $detailArr = AdminAccountType::find($id);

      return View::make('backend.category.edit')->with('detailArr',$detailArr);
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  int  $id
   * @return Response
   */
  public function update($id)
  {
      $dataArr = Input::except('_token');

      $ruleArr = array(
        "code" => "required"        
      );   

      $valid = Validator::make($dataArr,$ruleArr);

      if ($valid->passes()) {

          $model= AdminAccountType::find($id);          
          $model->code = Input::get('code');
          $model->description = Input::get('description');                    
          $model->save();

          return Redirect::route('admin.backend.category.index');

      } else {
         return Redirect::route('admin.backend.category.create')->withErrors($valid);
      }
      
  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  int  $id
   * @return Response
   */
  public function destroy($id)
  {
      $model= AdminAccountType::find($id);      
      $model->delete();
      return Redirect::route('admin.backend.category.index');
  }

  /**
   * ajax check code is in use
   *
   * @param  int $hotel_id, string $code , int $id
   * @return json
   */
  public function ajaxCheckCode() {
      $resultArr = array();
      $hotel_id = Input::get('hotel_id',0);
      $code = Input::get('code');

      $id = Input::get('id',0);

      $response = array();

      if ($code && $hotel_id >= 0 ) {
          
          $resultArr = AdminAccountType::where('hotel_id','=', $hotel_id)
                          ->where('code','=',$code)
                          ->where('id' , '<>' ,$id)->first();

          if ($resultArr == null) {
              $response['status'] = 'success';
          } else {
              $response['status'] = 'error';
          }
      }
      return Response::json($response);
  }

  /**
   * ajax update all order of code
   *
   * @param  int $hotel_id, string $str_id_order
   * @return null
   */
  public function ajaxUpdateOrder() {      
      $hotel_id = Input::get('hotel_id',0);
      $str_id_order = Input::get('str_id_order');
      if ($hotel_id >= 0 && $str_id_order != '') {                   
          $tmpArr = explode(";",$str_id_order);
          $i = 0;
          foreach ($tmpArr as $id) {            
              
              if ($id != '') {
                  $i++;
                  $model = AdminAccountType::find($id);
                  $model->ordering = $i;
                  $model->save();
              }
          }
      }      
  }


}